//
//  TripDetailDayItem.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/13.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "TripDetailDayItem.h"
#import <BaiduMapAPI/BMapKit.h>

@interface TripDetailDayItem()<BMKRouteSearchDelegate>
@property (nonatomic, strong) BMKRouteSearch *search;
@end
@implementation TripDetailDayItem


-(void)setNextDayItem:(TripDetailDayItem *)nextDayItem{
    _nextDayItem = nextDayItem;
    BMKRouteSearch *search = [[BMKRouteSearch alloc]init];
    self.search = search;
    search.delegate = self;
    
    BMKPlanNode * start = [[BMKPlanNode alloc]init];
    start.pt = CLLocationCoordinate2DMake(self.lat,self.lng);
    BMKPlanNode *end = [[BMKPlanNode alloc]init];
    end.pt = CLLocationCoordinate2DMake(self.nextDayItem.lat,self.nextDayItem.lng);
    
    
    BMKDrivingRoutePlanOption *transitRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;

    BOOL flag = [self.search drivingSearch:transitRouteSearchOption];
    if (flag) {
        self.navStr = @" ";
    }
//    [self performSelector:@selector(searchLine) withObject:nil afterDelay:3];
}


-(void)searchLine{

}


/**
 *返回驾乘搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果，类型为BMKDrivingRouteResult
 *@param error 错误号，@see BMKSearchErrorCode
 */
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        BMKDrivingRouteLine *taxiInfo = [result.routes firstObject];
        NSString *distance = @"";
        if (taxiInfo.distance>1000) {
            distance = [NSString stringWithFormat:@"%.1f公里",taxiInfo.distance/1000.0];
        }else{
            distance = [NSString stringWithFormat:@"%d米",taxiInfo.distance];
        }
        NSMutableString *duration = [NSMutableString string];
        if (taxiInfo.duration.hours!=0) {
            [duration appendFormat:@"%d小时",taxiInfo.duration.hours];
        }
        if (taxiInfo.duration.minutes!=0) {
            [duration appendFormat:@"%d分钟",taxiInfo.duration.minutes];
        }
        self.navStr = [NSString stringWithFormat:@"驾车%@，约%@",distance,duration];
        NSLog(@"self.navStr=%@",self.navStr);
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotiTebleViewReload object:nil];
    }
    searcher.delegate = nil;
    self.search = nil;
}

@end
